Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
reactive-property
Advanced tools
A small library for getter-setter functions that react to changes.
A small library for getter-setter functions that react to changes.
The pattern for creating reusable data visualizations described in Towards Reusable Charts is great. However, the boilerplate code for getter-setter functions is a bit cumbersome. This library creates chainable getter-setter functions so you don't have to. For more information, see "Introducing reactive-property" on Medium.
Here's a code example from Towards Reusable Charts showing the general pattern with width and height as example chainable getter-setters.
function chart() {
var width = 720, // default width
height = 80; // default height
function my() {
// generate chart here, using `width` and `height`
}
my.width = function(value) {
if (!arguments.length) return width;
width = value;
return my;
};
my.height = function(value) {
if (!arguments.length) return height;
height = value;
return my;
};
return my;
}
Here's how the above code can be transformed when using reactive-property.
function chart() {
function my() {
// generate chart here, using `my.width()` and `my.height()`
}
my.width = ReactiveProperty(720);
my.height = ReactiveProperty(80);
return my;
}
Pros:
Cons:
my.width()
instead of simply width
).For more detailed example code, have a look at the tests.
If you are using NPM, install the library by running this command.
npm install reactive-property
Require it in your code like this.
var ReactiveProperty = require("reactive-property");
You can also require the script in your HTML like this.
<script src="//datavis-tech.github.io/reactive-property/reactive-property-v0.9.0.js"></script>
Or use the minified version.
<script src="//datavis-tech.github.io/reactive-property/reactive-property-v0.9.0.min.js"></script>
Try reactive-property in your browser.
# ReactiveProperty([defaultValue])
Create a property by invoking ReactiveProperty as a constructor function.
var reactiveProperty = ReactiveProperty();
If defaultValue is specified, it is set as the default value of the property. This value can be any type.
var a = ReactiveProperty(3); // The default value is 3.
# reactiveProperty([value])
If value is specified, sets the value of the property. Returns the context object for method chaining.
a(5);
If value is not specified, returns the value of the property.
a(); // Returns 5
# reactiveProperty.default()
Returns the defaultValue specified in the constructor. If, defaultValue was not specified in the constructor, then the default function is not defined.
The expected usage pattern is to always check if default is defined before invoking it, like this.
if(a.default){
console.log(a.default());
}
Set up chainable setters by using a context object (called my
in this example).
var my = {
x: ReactiveProperty(5),
y: ReactiveProperty(10)
};
my.x(50).y(100); // Chainable setters.
You can also set up properties this way.
var my = {};
my.x = ReactiveProperty(5);
my.y = ReactiveProperty(10);
my.x(50).y(100);
# reactiveProperty.on(listener)
Listens for changes in the property value. Returns listener.
The listener callback function will be invoked synchronously when the property value is set. If the property has a default value that is not undefined
, then listener is invoked immediately. The special value null
is considered a defined value and is passed into listeners, whereas setting a property value to undefined
does not cause the listener to be invoked.
Arguments to the callback function - listener(value, oldValue)
undefined
if the value was not previously set).a.on(function(value, oldValue){
console.log("The value of 'a' changed from " + oldValue + " to " + value);
});
# reactiveProperty.off(listener)
Removes the given listener function that was previously added with on.
function listener(){
console.log("'a' changed!");
}
a.on(listener);
a.off(listener);
a(5); // The listener is NOT called.
Since listener is returned from on, the following pattern also works.
var listener = a.on(function (){
console.log("'a' changed!");
});
a.off(listener);
a(5); // The listener is NOT called.
# reactiveProperty.destroy()
Removes all listeners previously added with on. Use this to avoid memory leaks in cases where you know properties will no longer be used.
a.on(function (){ console.log("'a' changed!"); });
a.on(function (){ console.log("'a' really changed!"); });
a.destroy(); // Removes all listeners.
a(5); // No listeners are called.
Reactive-model performs pretty well as compared to other implementations of the Observer and Event Emitter patterns.
Data from benchmarks by @Hypercubed, found at Hypercubed/EventsSpeedTests node-v4.4.4.
Similar Libraries:
FAQs
A small library for getter-setter functions that react to changes.
We found that reactive-property demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.